package com.gxuwz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gxuwz.annotation.OperateAfterLog;
import com.gxuwz.annotation.OperateBeforeLog;
import com.gxuwz.constant.ConfigConsts;
import com.gxuwz.constant.UserOperateLogConsts;
import com.gxuwz.entity.User;
import com.gxuwz.service.IUserService;
import com.gxuwz.util.SessionUtils;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘玉霜
 * @since 2017-10-27
 */
@Controller
@RequestMapping("/user")
public class UserController extends AbstractIframe implements ICurdController<User>{
	
	private static Logger log = Logger.getLogger(UserController.class);
	
	@Autowired
	private IUserService userService;
	private Map<String,Object> json = null;
	
	public UserController(){
		json = new HashMap<String,Object>();
	}
	
	@Override
	public String list(Model model,User u){
		if(null == u){
			u = new User();
		}
		
		Page<User> page = new Page<User>(1,ConfigConsts.PAGE_SIZE);
		page = userService.selectPage(page);
		model.addAttribute("page", page);
		return "user/list";
	}
	
	@Override
	public String openAddView(){
		return "user/add";
	}
		
	@Override
	public Map<String,Object> add(User user,Model model){
		log.info("********** execute save user method **********");
		if(null == user){
			json.put("code", -1);
			return json;
		}	
		json.put("code", userService.save(user));
		return json;
	}

	@Override
	public Map<String, Object> delete(User user) {
		log.info("********** execute delete user method **********");
		json.put("code", userService.deleteById(user.getId()));
		return json;
	}
	
	/**
	 * 
	 * @Title: login 
	 * @Description: 登陆
	 * @param @param user
	 * @param @param model
	 * @param @return
	 * @return String
	 * @throws
	 */
	@OperateAfterLog(description = UserOperateLogConsts.LOGIN)
	@RequestMapping(value = "/login",method = RequestMethod.POST)
	public String login(User user,Model model){
		HttpSession session = SessionUtils.getSession();
		User userRecord = userService.login(user);
		boolean isExist = (userRecord == null ? false : true);
		if(isExist){
			log.info(user.getUsername() + " login success...");
			// 保存登陆的用户信息到session中
			session.setAttribute("loginUser",userRecord);
			return "common/main";
		}else{
			log.info(user.getUsername() + " login failure...");
			model.addAttribute("msg", "帐号或密码错误，请重新输入"); //向页面传值
			return "login"; 
		}	
	}
	
	/**
	 * 
	 * @Title: checkUsername 
	 * @Description: 检查用户名是否存在
	 * @param @param user
	 * @param @return
	 * @return boolean
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/checkUsername",method = RequestMethod.GET)
	public boolean checkUsername(User user){
		log.info("********** execute checkUsername method **********");
		List<User> users = userService.selectList(new EntityWrapper<User>().eq("username", user.getUsername()));
		if(users.size() == 0 ){
			return true;
		}
		return false;
	}
	
	/**
	 * 
	 * @Title: stopUse 
	 * @Description: 停用用户
	 * @param @param user
	 * @param @return
	 * @return Map<String,Object>
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/stopUse",method = RequestMethod.POST)
	public Map<String,Object> stopUse(User user){
		User u = userService.selectById(user.getId());
		u.setType(0);
		json.put("code", userService.updateById(u));
		return json;
	}
	
	/**
	 * 
	 * @Title: startUse 
	 * @Description: 启用用户
	 * @param @param user
	 * @param @return
	 * @return Map<String,Object>
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/startUse",method = RequestMethod.POST)
	public Map<String,Object> startUse(User user){
		User u = userService.selectById(user.getId());
		u.setType(1);
		json.put("code", userService.updateById(u));
		return json;
	}

	@Override
	public String openUpdateView(String id,Model model) {
		User u = userService.selectById(id);
		model.addAttribute("user", u);	
		return "user/update";
	}

	@Override
	public Map<String, Object> update(User user) {
		log.info("********** execute update method **********");
		if(null == user){
			json.put("code", -1);
			return json;
		}	
		json.put("code", userService.updateById(user));
		return json;
	}
	
	/**
	 * 
	 * @Title: logout 
	 * @Description: 退出登陆
	 * @param @return
	 * @return String
	 * @throws
	 */
	@OperateBeforeLog(description = UserOperateLogConsts.LOUGHT)
	@RequestMapping(value = "/logout")
	public String logout(Model model){
		HttpSession session = SessionUtils.getSession();
		session.removeAttribute("loginUser");
		model.addAttribute("msg", "退出登陆成功"); //向页面传值		
		return "login";
	}
	
}
